﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace ALDIOrganizer
{
    /// <summary>
    /// Used by Sheet class: its purpose is to keep track of most popular Shifts.
    /// </summary>
    /// <remarks>
    /// </remarks>
    [Serializable]
    class TopShiftsTracker
    {
        public TopShiftsTracker()
        {
            TopShifts = new ListDictionary();
        }

        public void ImportData(Sheet pRefill)
        {
            TopShifts.Clear();
            foreach (var shift in pRefill.GetAllShifts)
            {
                Add(shift);
            }
        }

        private ListDictionary TopShifts;

        public SortedDictionary<int, List<ShiftStub>> GetMostPopular
        {
            get
            {
                var result = new SortedDictionary<int, List<ShiftStub>>();
                foreach (DictionaryEntry entry in TopShifts)
                {
                    if (result.Keys.Contains((int)entry.Value))
                    {
                        result[(int)entry.Value].Add(
                            (ShiftStub)entry.Key);
                        continue;
                    }
                    // no key yet
                    var list = new List<ShiftStub> {(ShiftStub) entry.Key};
                    result.Add(
                        (int)entry.Value,
                        list);                
                }
                return result;
            }
        }

        public void Add(Shift pShift)
        {
            AddRemoveShiftForStats(pShift, true);
        }

        public void Remove(Shift pShift)
        {
            AddRemoveShiftForStats(pShift, false);
        }

        /// <summary>
        /// adds/removes Shift from the "top choices" list
        /// </summary>
        /// <param Name="pShift">the Shift in question</param>
        /// <param Name="AddAnotherRowString">are you adding or removing? true = AddAnotherRowString / false = remove</param>
        private void AddRemoveShiftForStats(Shift s, bool add)
        {
            var _shForStats = new ShiftStub();
            _shForStats = s.ToShiftStub();
            bool exists = TopShifts.Contains(_shForStats);
            int howMany = 0;
            if (exists)
            {
                howMany = (int)TopShifts[_shForStats];

                if (add || howMany > 1)
                {
                    TopShifts[_shForStats] = (int)this.TopShifts[_shForStats] + (add ? 1 : -1);
                }
                if (!add & howMany == 1)
                {
                    // and we're NOT adding
                    TopShifts.Remove(_shForStats);
                }
                
                return;
            }            
            if (add) // and key does not exist
            {
                TopShifts.Add(_shForStats, 1);
            }
            DebListTopShifts();
        }

        private string DebListTopShifts()
        {
            string result = String.Empty;
            foreach(var key in TopShifts.Keys)
            {
                if (!TopShifts.Contains(key))
                {
                    Debugger.Break();
                }
                if (key is ShiftStub)
                {
                    result += (key as ShiftStub).ToString();
                }
                else
                {
                    continue;
                }
                result += String.Concat(
                    ": ",
                    (int) TopShifts[key],
                    Environment.NewLine);
            }
            return result;
        }
    }
}
